package com.zb.springevent.controller;

import com.zb.springevent.event.GoodsAddEvent;
import com.zb.springevent.event.UserAddEvent;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author zb
 * @Date 2025/6/16 13:24
 */

@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController {
    @Resource
    private ApplicationContext applicationContext;


    @RequestMapping("/add")
    public String add(String name) {
        this.notifyEvent(name);
        return "goods add success";
    }


    private void notifyEvent(String name) {
        GoodsAddEvent goodsAddEvent = new GoodsAddEvent(this);
        goodsAddEvent.setTopic("goods");
        goodsAddEvent.setPayload(name);
        applicationContext.publishEvent(goodsAddEvent);
    }
}
